package com.suyang.syt.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.suyang.syt.auth.AuthClass;
import com.suyang.syt.auth.AuthMethod;
import com.suyang.syt.model.Pic;
import com.suyang.syt.model.User;
import com.suyang.syt.model.dto.AjaxObj;
import com.suyang.syt.model.dto.BannerDTO;
import com.suyang.syt.model.dto.PagerEasyUI;
import com.suyang.syt.service.interfaces.BannerService;
import com.suyang.syt.service.interfaces.PicService;
import com.suyang.syt.util.DateFormat;
import com.suyang.syt.util.EmptyUtil;
import com.suyang.syt.util.StringUtil;

@Controller
@RequestMapping("/admin/banner")
@AuthClass("login")
public class AdminBannerController {
	@Autowired
	private BannerService bannerService;
	@Autowired
	private PicService picService;

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list(String objId, String flag, String opterType, Model model) {
		if (EmptyUtil.isNotEmpty(objId)) {
			model.addAttribute("objId", objId);
		}
		if (EmptyUtil.isNotEmpty(flag)) {
			model.addAttribute("flag", flag);
		}
		if (EmptyUtil.isNotEmpty(opterType)) {
			model.addAttribute("opterType", opterType);
		} else {
			model.addAttribute("opterType", 0);
		}
		return "banner/list";
	}

	@AuthMethod(role = "ROLE_PXJG,ROLE_JGJS")
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	public PagerEasyUI<BannerDTO> list(HttpServletRequest request, int page, int rows, String sort, String order) {
		try {

			String createDate1 = request.getParameter("createDate1");
			String createDate2 = request.getParameter("createDate2");
			long objId = Long.parseLong(request.getParameter("objId"));
			int flag = Integer.parseInt(request.getParameter("flag"));
			int opterType = Integer.parseInt(request.getParameter("opterType"));
			return bannerService.listByEasyUI(page, rows, sort, order, objId, flag, opterType, createDate1, createDate2);
		} catch (Exception e) {
			return null;
		}
	}

	@AuthMethod(role = "ROLE_PXJG,ROLE_JGJS")
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	public AjaxObj add(HttpServletRequest request, BannerDTO bannerDTO) {
		try {
			User user = (User) request.getSession().getAttribute("loginUser");
			bannerDTO.setCreateName(user.getUsername());
			bannerDTO.setCreateTime(DateFormat.dateToString(new Date()));
			return bannerService.add(bannerDTO);
		} catch (Exception e) {
			return new AjaxObj(0, StringUtil.ERROR_MSG);
		}
	}

	@AuthMethod(role = "ROLE_PXJG,ROLE_JGJS")
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	@ResponseBody
	public AjaxObj delete(HttpServletRequest request, String ids) {
		try {
			return bannerService.deletes(ids);
		} catch (Exception e) {
			return new AjaxObj(0, StringUtil.ERROR_MSG);
		}
	}

	@AuthMethod(role = "ROLE_PXJG,ROLE_JGJS")
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@ResponseBody
	public AjaxObj update(HttpServletRequest request, BannerDTO bannerDTO) {
		try {
			User user = (User) request.getSession().getAttribute("loginUser");
			bannerDTO.setModifyName(user.getUsername());
			bannerDTO.setModifyTime(DateFormat.dateToString(new Date()));
			return bannerService.update(bannerDTO);
		} catch (Exception e) {
			return new AjaxObj(0, StringUtil.ERROR_MSG);
		}
	}

	@AuthMethod(role = "ROLE_PXJG,ROLE_JGJS")
	@RequestMapping(value = "/updatePic", method = RequestMethod.POST)
	@ResponseBody
	public AjaxObj updatePic(HttpServletRequest request, String orderIndex, String bid, String picPath) {
		try {
			return bannerService.updatePic(bid, orderIndex, picPath);
		} catch (Exception e) {
			return new AjaxObj(0, StringUtil.ERROR_MSG);
		}
	}

	@AuthMethod(role = "ROLE_PXJG,ROLE_JGJS")
	@RequestMapping(value = "/findByBanner", method = RequestMethod.POST)
	@ResponseBody
	public List<Pic> findByBanner(HttpServletRequest request, Long bid) {
		try {
			return picService.findByBanner(bid);
		} catch (Exception e) {
			return null;
		}
	}

}
